package com.itboye.ihomebank.activity.me;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.itboye.ihomebank.R;
import com.itboye.ihomebank.adapter.ZhangHuAdapter;
import com.itboye.ihomebank.base.BaseOtherActivity;
import com.itboye.ihomebank.bean.BindCardsList;
import com.itboye.ihomebank.bean.ResultEntity;
import com.itboye.ihomebank.constants.SPContants;
import com.itboye.ihomebank.activity.myzhujia.qianbao.ActivityAddBankCard;
import com.itboye.ihomebank.presenter.UserPresenter;
import com.itboye.ihomebank.util.ByAlert;
import com.itboye.ihomebank.util.SPUtils;
import com.itboye.ihomebank.util.UIAlertView;

import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;

/**
 * 我的账户
 * 
 */
public class ActivityZhangHu extends BaseOtherActivity implements Observer {
	ImageView zhanghu_icon;

	ListView zhanghu_listView;
	ImageView zhanghu_add;
    TextView qureyBank;
	ZhangHuAdapter adapter;

	UserPresenter userPresenter;

	private ArrayList<BindCardsList> arrayList;

	private String uid;

	int type = 0;// 1：查看绑定银行卡列表 2：选择银行卡返回

	LinearLayout addcard;

	private UIAlertView uiAlertView;

	protected String bankNo;
	public static ActivityZhangHu context;

	public static ActivityZhangHu getInstance() {
		return context;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.zhanghu_icon:
			finish();
			break;
		case R.id.zhanghu_add:// 添加银行卡
			Intent intent = new Intent(this, ActivityAddBankCard.class);
			startActivity(intent);

			break;
			case R.id.qureyBank:
				Uri uri = Uri.parse("http://api.91renta.com/public/webview.php/pabanklimit");
				Intent inte = new Intent(Intent.ACTION_VIEW, uri);
				startActivity(inte);
				break;
		default:
			break;
		}

	}

	@Override
	protected int layout_id() {
		return R.layout.activity_zhang_hu;
	}

	@Override
	protected void onInitController() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void onInit() {
		context = this;
		type = getIntent().getIntExtra("type", type);
		userPresenter = new UserPresenter(this);
		refresh();
		arrayList = new ArrayList<>();
		adapter = new ZhangHuAdapter(this, arrayList, R.layout.item_zhanghu);
		zhanghu_listView.setAdapter(adapter);
		zhanghu_listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				BindCardsList bindCardsList = arrayList.get(position);
				if (type == 1) {
					if(bindCardsList.getBank().get(2).equals("贷记卡")){
						ByAlert.alert("充值,提现不能使用信用卡");
					}else{
						Intent intent = new Intent();
						intent.putExtra("model", bindCardsList);
						setResult(102, intent);
						finish();
					}
				}else{
					Intent intent = new Intent();
					intent.putExtra("model", bindCardsList);
					setResult(102, intent);
					finish();
				}
			}
		});
		zhanghu_listView
				.setOnItemLongClickListener(new OnItemLongClickListener() {

					@Override
					public boolean onItemLongClick(AdapterView<?> parent,
							View view, int position, long id) {
						// TODO Auto-generated method stub
						bankNo = arrayList.get(position).getBankNo();
						showDelDialog();
						return true;
					}
				});
	}

	private void showDelDialog() {
		final String uid = SPUtils.get(this, null, SPContants.USER_ID, "") + "";
		uiAlertView = new UIAlertView(this, "操作提示！", "是否解除当前银行卡的绑定？", "取消",
				"确定");
		uiAlertView.show();

		uiAlertView.setClicklistener(new UIAlertView.ClickListenerInterface() {

			public void doLeft() {
				uiAlertView.dismiss();
			}

			public void doRight() {
				userPresenter.unbindBankCard(uid, bankNo);
			}
		});
	}

	@Override
	protected void onReadIntent(Bundle bundle) {
		// TODO Auto-generated method stub

	}

	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		ResultEntity entity = handlerError(data);
		if (entity != null) {
			if (entity.getEventType() == UserPresenter.bindCardList_success) {
				arrayList.clear();
				arrayList.addAll((ArrayList<BindCardsList>) entity.getData());
				adapter.notifyDataSetChanged();
			} else if (entity.getEventType() == UserPresenter.bindCardList_fail) {
				ByAlert.alert(entity.getData());
			} else if (entity.getEventType() == UserPresenter.unbindCardList_success) {
				userPresenter.bankList(uid);
				uiAlertView.dismiss();
				ByAlert.alert(entity.getData());
			} else if (entity.getEventType() == UserPresenter.unbindCardList_fail) {
				ByAlert.alert(entity.getData());
				uiAlertView.dismiss();
			}
		}

	}

	public void refresh() {
		// TODO Auto-generated method stub
		uid = SPUtils.get(this, null, SPContants.USER_ID, "") + "";
		userPresenter.bankList(uid);
	}

}
